package fons.model.ontology.calculation;

import java.util.Collections;
import java.util.Map;

import com.google.common.collect.Maps;

import fons.model.ontology.Dimension;
import fons.model.ontology.Value;

public class Variable {

	private final Map<String, Variable> boundVariables = Maps.newHashMap();
	private final Map<String, Dimension> constraints = Maps.newHashMap();
	private final String name;
	private final VariableDefinition definition;
	private Value value;

	public Variable(String name, VariableDefinition definition) {
		this.name = name;
		this.definition = definition;
	}

	public VariableDefinition getDefinition() {
		return this.definition;
	}
	
	public String getName() {
		return this.name;
	}

	public void addConstraint(String first, Dimension second) {
		constraints.put(first, second);
	}

	public void setValue(Value value) {
		this.value = value;
	}

	public void bindVariable(Variable boundVariable) {
		this.boundVariables.put(boundVariable.getName(), boundVariable);
	}
	
	public Map<String, Variable> getBoundVariables () {
		return Collections.unmodifiableMap(this.boundVariables);
	}

	@Override
	public String toString() {
		return "Variable " + name + (boundVariables.size() > 0 ? ":\n\t[boundVariables=" + boundVariables + ",\n\t" : "") 
				+ " constraints="
				+ constraints + ", value=" + value.getValue() + "]";
	}

	public Map<String, Dimension> getNamedDimensions() {
		return constraints;
	}

	public Dimension getDimension(String constraintName) {
		return this.constraints.get(constraintName);
	}

	public double getValue() {
		return this.value.getValue();
	}
}
